<?php
/* $Id: orders.php 14 2009-04-11 17:40:34Z ronan $ */
$basesite=$_SERVER['PHP_SELF'];
require("common.php");
require("auth.php");
require("header.php");
$f=(isset($_GET['f'])?$_GET['f']:'');
if($f=='cancel') {
	$id=$_GET['id'];
	$order = Query("SELECT * FROM orders WHERE id='$id' LIMIT 1");
	if(mysql_result($order,0,'usingStock')==1) {
		$newQuantityRemaining=mysql_result($order,0,'quantity');
		$specialOrders = Query("SELECT * FROM orders WHERE typeID='".mysql_result($order,0,'typeID')."' AND ((usingStock=0 AND status='awaiting producer') OR (usingStock=2 AND status='awaiting processing')) ORDER BY timePlaced ASC");
		if(mysql_num_rows($specialOrders)>0) {
			while(($l=mysql_fetch_array($specialOrders)) && $newQuantityRemaining>0) {
				if($newQuantityRemaining>=$l['quantity']) {
					$newQuantityRemaining-=$l['quantity'];
					Query("UPDATE orders SET usingStock=1, status='awaiting processing' WHERE id='".$l['id']."' LIMIT 1");
				} else {
					$newOrderQuantity=$l['quantity']-$newQuantityRemaining;
					$newOrderPrice = $newOrderQuantity * $l['pricePerUnit'];
					Query("INSERT INTO orders(timePlaced, typeID, typeName, quantity, pricePerUnit, priceTotal, customerID, customerName, status, usingStock) VALUES('".$l['timePlaced']."', '".$l['typeID']."', '".$l['typeName']."', '$newOrderQuantity', '".$l['pricePerUnit']."', '$newOrderPrice', '".$l['customerID']."', '".$l['customerName']."', '".$l['status']."', '".$l['usingStock']."')");
					Query("UPDATE orders SET usingStock=1, status='awaiting processing', quantity=$newQuantityRemaining, priceTotal=quantity*pricePerUnit WHERE id='".$l['id']."' LIMIT 1");
					$newQuantityRemaining=0;
				}
			}
		}
		$itemresults = Query("UPDATE items SET numAvailable=numAvailable+".$newQuantityRemaining." WHERE typeID='".mysql_result($order,0,'typeID')."' LIMIT 1");
	}
	if(mysql_result($order,0,'usingStock')==3) $itemresults = Query("UPDATE blueprints SET numAvailable=numAvailable+".mysql_result($order,0,'quantity')." WHERE id='".mysql_result($order,0,'typeID')."' LIMIT 1");
	$results = Query("DELETE FROM orders WHERE id='$id' LIMIT 1");
	echo "Order cancelled.<br /><br />\n\n";
}
$orders = Query("SELECT * FROM orders WHERE customerID='$uidnum' AND status<>'in cart' ORDER BY status DESC, timePlaced ASC");
if(!mysql_num_rows($orders)) {
	echo "You have no orders!";
	require("footer.php");
	exit;
}
$orders = Query("SELECT * FROM orders WHERE customerID='$uidnum' AND status<>'completed' AND status<>'in cart' ORDER BY timePlaced DESC");
echo "<h3>Outstanding Orders</h3><br />\n";
if(!mysql_num_rows($orders)) {
	echo "You do not have any outstanding orders.<br /><br />";
} else {
	echo "<table border=0 cellspacing=0 cellpadding=2><tr><td><b>Placed</b></td><td><b>Item</b></td><td><b>Qty</b></td><td><b>Total</b></td><td><b>Status</b></td><td><b>Type</b></td><td></td></tr>\n";
	while ($line = mysql_fetch_array($orders, MYSQL_ASSOC)) {
		echo ($bgcolor?"<tr class=\"tablecolor1\">":"<tr class=\"tablecolor2\">");
		$bgcolor= !$bgcolor;
		echo "\t" .
    			"<td>".dateTime($dateTimeFormat,$line['timePlaced'])."</td>\t" .
    			"<td>".$line['typeName']."</td>\t" .
    			"<td>".addCommas($line['quantity']).($line['quantityModifier']==1?'':"x".addCommas($line['quantityModifier']))."</td>\t" .
    			"<td>".addCommas($line['priceTotal'])." ISK</td>\t" .
    			"<td>".ucwords($line['status'])."</td>\t" .
    			"<td>".getColoredType($line['usingStock'])."</td>\t" .
    			"<td>".(($line['status']=='awaiting producer'||$line['status']=='awaiting processing')?generateButton("Cancel Order", "GET", array("f"=>"cancel", "id"=>$line['id'])):'')."</td>" .
    			"</tr>\n";
	}
	echo "</table><br />\n<br />";
}
$orders = Query("SELECT * FROM orders WHERE customerID='$uidnum' AND status='completed' ORDER BY timeCompleted DESC LIMIT 10");
echo "<br /><br />\n\n<h3>Last 10 Completed Orders</h3><br />\n";
if(!mysql_num_rows($orders)) {
	echo "You have not completed any orders.";
	require("footer.php");
	exit;
}
echo "<table border=0 cellspacing=0 cellpadding=2><tr><td><b>Placed</b></td><td><b>Completed</b></td><td><b>Item</b></td><td><b>Qty</b></td><td><b>Total</b></td><td>Type</td></tr>\n";
$bgcolor=false;
while ($line = mysql_fetch_array($orders, MYSQL_ASSOC)) {
	echo ($bgcolor?"<tr class=\"tablecolor1\">":"<tr class=\"tablecolor2\">");
	$bgcolor= !$bgcolor;
	echo "\t" .
    	"<td>".dateTime($dateTimeFormat,$line['timePlaced'])."</td>\t" .
    	"<td>".dateTime($dateTimeFormat,$line['timeCompleted'])."</td>\t" .
    	"<td>".$line['typeName']."</td>\t" .
    	"<td>".addCommas($line['quantity']).($line['quantityModifier']==1?'':"x".addCommas($line['quantityModifier']))."</td>\t" .
    	"<td>".addCommas($line['priceTotal'])." ISK</td>\t" .
    	"<td>".getColoredType($line['usingStock'])."</td>\t" .
   		"</tr>\n";
}
echo "</table>";
require("footer.php");
?>

